package component.renderers
{
	import flash.display.GradientType;

	import mx.containers.Canvas;
	import mx.controls.Label;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IDropInListItemRenderer;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;

	import vo.ArmoryBean;

	public class RendererDataBar extends Canvas implements IDropInListItemRenderer
	{
		private var _label:Label;
		private var _bar:UIComponent;

		public function RendererDataBar()
		{
			super();

			super.horizontalScrollPolicy = "false";
			super.verticalScrollPolicy = "false";
			
			_bar = new UIComponent();
			this.addChild(_bar);

			_label = new Label();
			this.addChild(_label);

			this.addEventListener(FlexEvent.UPDATE_COMPLETE, updateCompleteHandler);
		}

		private function updateCompleteHandler(event:FlexEvent):void
		{
			_label.x = (this.width - _label.width) / 2;
			_label.y = (this.height - _label.height) / 2;
		}
		private var _dataFields:Array;
		private var _params:Object;

		public function set dataFields(data:String):void
		{
			// _params
			if(data.split("?").length > 1)
			{
				_params = {};
				var params:Array = (data.split("?")[1] as String).split("&");
				for each(var param:String in params)
				{
					_params[param.split("=")[0]] = param.split("=")[1];
				}

			}
			// _dataFields
			data = data.split("?")[0];
			_dataFields = data.split(".");
		}

		private var _itemPropertyBean:ArmoryBean;

		private var _listData:AdvancedDataGridListData;

		public function set listData(value:BaseListData):void
		{
			// 拖拽时
			if((value as AdvancedDataGridListData).item == null)
				return;

			_listData = value as AdvancedDataGridListData;

			_itemPropertyBean = _listData.item as ArmoryBean;

			var temp:Object = _itemPropertyBean;
			for each(var field:String in _dataFields)
			{
				temp = temp[field];
			}
			_label.text = int(temp).toString();

			_bar.graphics.clear();
			_bar.graphics.beginGradientFill(GradientType.LINEAR, [0x0000ff, 0xffffff], [1, 1], [32, 255]);
			if(this.width > 0)
				WIDTH = this.width;
			_bar.graphics.drawRect(2, 2, int(temp) / int(_params.max) * (WIDTH - 4), HEIGHT - 4);
			_bar.graphics.endFill();
		}
		private static var WIDTH:Number = 0;
		private static const HEIGHT:Number = 22;

		public function get listData():BaseListData
		{
			return _listData;
		}
	}
}
